var acActionStore;
var acActionGrid;
var acActionWindow;
var sm;
var selectedRecords = new Array();
var selectedIds = new Array();

var selectAcAction = function(selectCallback){
    if (!acActionWindow) {
        acActionWindow = new Ext.Window({
            title: message.get('message.combo.pleaseselect'),
            layout: 'fit',
            width: 760,
            height: 550,
            closeAction: 'hide',
            plain: true,
            modal: true,
            items: [{
                id: 'buttonPanel',
                region: 'center',
                bodyStyle: {
                    background: '#ffffff',
                    padding: '7px'
                },
                html: '<div id="acActonDiv"></div>'
            }]
        });
		
		acActionWindow.on(
			'beforehide',
			function(){
				selectAcActionCallBack(selectedIds);
			}
		);
		
    }
    acActionWindow.show();
    
    if (acActionGrid) 
        return;
    acActionStore =  new Ext.data.Store({
        url: '../../servlet/AcActionServlet?action=4',
        
        reader: new Ext.data.XmlReader({
            record: 'item',
            id: 'id',
            totalRecords: 'itemTotal'
        }, [ 
        'index', 'id', 'actionUrl', 'actionNameCn', 'actionNameFr', 'actionNameEn']),
        
        remoteSort: true
    });
	
	acActionStore.on(
		'load',
		function(store, records, options){
			var indexes = new Array();
			for(var i = 0; i < records.length; i ++){
				if(selectedIds.indexOf(records[i].data.id) != -1){
					indexes[indexes.length] = i;
				}
			}
			sm.selectRows(indexes);
		}
	);
    
    acActionStore.setDefaultSort('id', 'desc');
	
	var acActionFilters = new Ext.grid.GridFilters({
		  filters:[
		    {type: 'string',  dataIndex: 'actionUrl'},
			{type: 'string',  dataIndex: 'actionNameCn'},
			{type: 'string',  dataIndex: 'actionNameFr'},
			{type: 'string',  dataIndex: 'actionNameEn'}
	]});

	sm = new Ext.grid.CheckboxSelectionModel();
	sm.on(
		'rowselect',
		function(thisSM, rowIndex, record){
			if(selectedIds.indexOf(record.data.id) == -1){
				selectedIds[selectedIds.length] = record.data.id;
				//selectedRecords[selectedRecords.length] = '(' + record.data.seasonNumber + ', ' + record.data['biReference.reference'] + ', ' + record.data.orderNumber + ')';
			}
		}
	);
	
	sm.on(
		'rowdeselect',
		function(thisSM, rowIndex, record){
			selectedIds.remove(record.data.id);
			//selectedRecords.remove('(' + record.data.seasonNumber + ', ' + record.data['biReference.reference'] + ', ' + record.data.orderNumber + ')');
		}
	);
    // create the grid
    acActionGrid = new Ext.grid.GridPanel({
        title: acMessage.get('ui.accesscontrol.listaction'),
        store: acActionStore,
        stripeRows: true,
        plugins: [acActionFilters],
        height: 560,
		sm: sm,
        columns: [sm,{
            header: message.get('message.list.id'),
            width: 50,
            dataIndex: 'index',
            sortable: false
        }, {
            header: acMessage.get('ui.accesscontrol.actionurl'),
            dataIndex: 'actionUrl',
            sortable: true
        }, {
            header: message.get('message.edit.cn'),
            dataIndex: 'actionNameCn',
            sortable: true
        }, {
            header: message.get('message.edit.fr'),
            dataIndex: 'actionNameFr',
            sortable: true
        }, {
            header: message.get('message.edit.en'),
            dataIndex: 'actionNameEn',
            sortable: true
        }],
        viewConfig: {
            forceFit: true
        },
        loadMask: true,
        renderTo: 'acActonDiv',
        bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: acActionStore,
            displayInfo: true
        })
    });
    
    var startPage = getPrarmeterValue(window.location, 'startPage');
    var start;
    if (startPage == null || startPage == '') {
        start = 0;
    }
    else {
        start = (startPage - 1) * 20;
    }
    acActionStore.load({
        params: {
            start: start,
            limit: 20
        }
    });
}

